import * as fs from 'fs-extra';
import { logger } from './logger';
export class JsonFileUtility {
  public static serializeToFile<T>(filePath: string, data: T): void {
    fs.ensureFileSync(filePath);
    fs.writeJsonSync(filePath, data);
  }

  public static deserializeFromFile<T>(path: string): T | undefined;
  public static deserializeFromFile<T>(path: string, defaultValue: T): T;
  public static deserializeFromFile<T>(path: string, defaultValue?: T): T | undefined {
    try {
      return fs.readJsonSync(path);
    } catch (e) {
      logger.warn(`❗️read json file: ${path} failed. ${e.message}`);
      return defaultValue;
    }
  }
}